package controller;

//package controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

import data.*;

/**
 * Servlet implementation class Login
 */
@WebServlet("/Login")
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    @Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
    @Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
            String user = request.getParameter("user");
            String pwd = request.getParameter("password");
            
            byte[] bytPassword = pwd.getBytes();
            MessageDigest md = null;
            byte[] hashPwd = null;
			try {
				md = MessageDigest.getInstance("MD5");
				hashPwd = md.digest(bytPassword);
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            
            System.out.println(user + " " + pwd);
            PrintWriter out = response.getWriter();
            HttpSession session = request.getSession();
            ArrayList<UserInfo> userList = null;
            
            UserInfo userinfo = null;
            
            ArrayList<String> defTbsList = null;
            ArrayList<String> tmpTbsList = null;
            ArrayList<String> profileList = null;
            ArrayList<String> roleList    = null;
            
    		
            //Connect to database
            Connect connection = new Connect();
            String mess = connection.connectData(Constant.OracleAdminUsr,Constant.OracleAdminPwd);
            
            if(mess.equals("success")){
                User_Login querry = new User_Login(connection.getConnection());
                userinfo = querry.querryUser(user.toUpperCase(), pwd);

                List_DefTablespace lst = new List_DefTablespace(connection.getConnection());
                defTbsList = lst.tblspaceLst();
                
                List_TmpTablespace lst1 = new List_TmpTablespace(connection.getConnection());
                tmpTbsList = lst1.tbspaceList();
                
                List_Profile lst2 = new List_Profile(connection.getConnection());
                profileList = lst2.profileList();
                
            }
            else {
                //System.out.println("Cannot connect to database");
                out.println("<h3><font color = #FF0000>"+mess+"</font></h3>");
                return;
            }
            
            if(userinfo != null){
            	List_User u = new List_User(connection.getConnection());
                userList = u.queryUserList(userinfo.getUsername().toUpperCase());
                List_Role lst3 = new List_Role(connection.getConnection());
                roleList      = lst3.roleList(userinfo.getUsername());
                connection.closeConnection();
//                request.setAttribute("user", userinfo);
//                request.setAttribute("userList", userList);
                System.out.println("Login.java : " + userinfo.getDefTbspace());
                System.out.println("Profile = " + userinfo.getProfile());
                session.setAttribute("user", userinfo);
                session.setAttribute("userList", userList);
                session.setAttribute("defTbsList", defTbsList);
                session.setAttribute("tmpTbsList", tmpTbsList);
                session.setAttribute("profileList", profileList);
                session.setAttribute("roleList", roleList);
                
                //System.out.println(userinfo.getUsername());
                //System.out.println(((UserInfo)session.getAttribute("user")).getUsername());
                System.out.println("-------------------------");
                        
                String url = "/UserInfo.jsp";
                RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher(url);
                dispatcher.forward(request, response);
                
            }else{
            	connection.closeConnection();
                response.sendRedirect("login_err.jsp");
            }
            
	}
}